<?php

namespace app\admin\controller;

use think\Controller;

class System extends Controller
{

    //结算
    public function count_profit()
    {
       //echo strtotime(date('Y-m-d').'23:59');
        $Projectitem = model('Projectitem');
            $items = $Projectitem->getToDayItem();
        //获取今日所有打卡项
        if(!$items){//找不到就终止
            return;
        }
        //print_r($items);exit;
        $signed = [];//已签到人
        $notsign = [];//未签到人
        //拆分已打卡与未打卡
        foreach ($items as $k => $i) {
            if ($i['status'] == 0) {
                $notsign[] = $i;
            } else {
                $signed[] = $i;
            }
        }
        //已打卡的未打卡的占比
        $total = count($items);
        $signedsum = count($signed);
        $signedper = round(($signedsum/$total)*100);
        $unsignedper = 100 - $signedper;
        $arr = [
            'signed_count' => $signedper,
            'notsign_count' => $unsignedper
        ];
        model('System')->save($arr,['id' =>1]);
        //未打卡总金额
        $nosign_count = $this->count_notsign($notsign);
        //瓜分总份数
        $split_count = $this->signed_split_count($signed);

        if($split_count==0){
            return;
        }

        //print_r($split_count);exit;
        //每份金额
        $split_money = $nosign_count/$split_count;
        //记录奖金到打卡项中
        $this->add_bonus($signed, $split_money);
        //结束未打卡项目
        $this->overProject($notsign);
        //dump($nosign_count);
    }

    //计算未打卡总金额
    private function count_notsign($notsign)
    {
        $total = 0;//总金额
        foreach ($notsign as $k => $i) {
            $total += $i['join_money'];
        }
        return $total;
    }

    //已打卡瓜分总份数计算
    private function signed_split_count($signed)
    {
        $total = 0;
        if (!empty($signed)){
            foreach ($signed as $k => $i) {
                $total += $i['uniy_count'];
            }
        }
        return $total;
    }

    //记录奖金到已打卡人打卡项，并且存入会员账户
    /*
     * $signed 已打卡项
     * $split_money 每份奖励金额
     */
    private function add_bonus($signed, $split_money)
    {
        $projectItem = model('Projectitem');
        $user=model('User');
        foreach ($signed as $k => $i) {
            $money = $i['uniy_count'] * $split_money;
            $leftmoney = $this->UserSliceprofit($money,$i['uid']);
            if($leftmoney>=0){
                $res = $projectItem->where(array('id' => $i['id']))->update(array('money' => $leftmoney,'count_status'=>1));
                $user_res=$user->where(array('id'=>$i['uid']))->setInc('add_bonus',$leftmoney);
                $user->where(array('id'=>$i['uid']))->setInc('balance',$leftmoney);
                $data = [
                    'uid' => $i['uid'],
                    'user_id' => $i['uid'],
                    'create_time' => time(),
                    'money' => $leftmoney,
                    'content' => '父级分成,获利:' . $leftmoney. '元',
                    'level' => 0,
                ];
//                存入奖金日志
                db('userprofit_logs')->insert($data);
            }
        }
    }

    /*
     * 结束未打卡项的参与活动
     *$notsign 未打卡项
     */
    private function overProject($notsign)
    {
        $project = model('Project');
        foreach ($notsign as $k => $i) {
            $res = $project->overProjectById($i['pro_id']);
        }
    }

    /**
     * 用户三级分销奖金
     */
    public function UserSliceprofit($money,$userid){
        $config = model("System")->sysConfig();
        $rate = $config['profit_rate'];
        $user = model('User');
//        $userid= cookie('uid');
        $userinfo = $user->find($userid);
//        halt($userinfo);
        if($userinfo){

            if($userinfo['pid']!==0){
                //        第一级分销
                if($userinfo['fuid1']){
                    $money1 = $money*$rate/100;
//                    if($money1)
                    $user->where(array('id'=>$userinfo['fuid1']))->setInc('add_bonus',$money1);
                    $user->where(array('id'=>$userinfo['fuid1']))->setInc('balance',$money1);
                    $data1 = [
                        'uid' => $userid,
                        'user_id' => $userinfo['fuid1'],
                        'create_time' => time(),
                        'money' => $money1,
                        'content' => '一级分成,利率' . $rate . '获利:' . $money1 . '元',
                        'level' => 1,
                    ];
                    db('userprofit_logs')->insert($data1);

                    $leftmoney = $money - $money1;
                }
                if($userinfo['fuid2']){
                    $money2 = $leftmoney*$rate/100;
                    $user->where(array('id'=>$userinfo['fuid2']))->setInc('add_bonus',$money2);
                    $user->where(array('id'=>$userinfo['fuid2']))->setInc('balance',$money2);
                    $data2 = [
                        'uid' => $userid,
                        'user_id' => $userinfo['fuid2'],
                        'create_time' => time(),
                        'money' => $money2,
                        'content' => '二级分成,利率' . $rate . '获利:' . $money2 . '元',
                        'level' => 2,
                    ];
                    db('userprofit_logs')->insert($data2);
                    $leftmoney = $leftmoney - $money2;
                }
                if($userinfo['fuid3']){
                    $money3 = $leftmoney *$rate/100;
                    $user->where(array('id'=>$userinfo['fuid3']))->setInc('add_bonus',$money3);
                    $user->where(array('id'=>$userinfo['fuid3']))->setInc('balance',$money3);
                    $data3 = [
                        'uid' => $userid,
                        'user_id' => $userinfo['fuid3'],
                        'create_time' => time(),
                        'money' => $money3,
                        'content' => '三级分成,利率' . $rate . '获利:' . $money3 . '元',
                        'level' => 3,
                    ];
                    db('userprofit_logs')->insert($data3);
//                最后用户剩下应分的的钱
                    $leftmoney = $leftmoney - $money3;
                }
            }else{
                $leftmoney = $money;
            }
        }else{
            return false;
        }
      return $leftmoney;
    }

}